package me.imli.app.ui;

import me.imli.app.R;
import me.imli.app.util.ActivityUtil;
import me.imli.app.util.ThemeUtil;
import me.imli.app.util.ToastUtil;
import me.imli.app.util.Util;
import me.imli.app.volley.RequestListener;
import me.imli.app.volley.RequestManager;
import me.imli.app.volley.RequestParams;
import me.imli.app.widget.crouton.Style;
import me.imli.app.widget.swipeblack.SwipeBackActivity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class BaseActivity extends SwipeBackActivity {

	/** TAG */
	protected final String TAG = getClass().getName();
	
	protected Toolbar mToolbar;
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(ThemeUtil.getThemeId());
		ActivityUtil.add(this);
		initActionBar();
		translucent();
	}
	
	@TargetApi(Build.VERSION_CODES.KITKAT)
	private void translucent(){
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // Translucent status bar
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
	}
	
	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		initToast();
	}
	
	@Override
	public void setContentView(View view) {
		super.setContentView(view);
		initToast();
	}
	
	@Override
	public void setContentView(View view, LayoutParams params) {
		super.setContentView(view, params);
		initToast();
	}
	
	private void initToast() {
		ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
		LinearLayout layout = (LinearLayout) LinearLayout.inflate(getActivity(), R.layout.layout_toast, null);
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, Util.dip2px(getActivity(), 48)));
		viewGroup.addView(layout);
	}
	
	private void initActionBar() {
		mToolbar = (Toolbar) Toolbar.inflate(getActivity(), R.layout.toolbar_default, null);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        
        // 添加到View上
        ViewParent vp = getWindow().getDecorView().findViewById(android.R.id.content).getParent();
        if (vp instanceof ViewGroup) {
        	((ViewGroup) vp).addView(mToolbar, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }
	}
	
	protected Activity getActivity() {
		return this;
	}
	
	/**
	 * 打开一个新的Activity
	 * @param clazz
	 */
	protected void openActivity(Class<?> clazz) {
		try {
			Intent intent = new Intent(this, clazz);
			startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 打开一个新的Activity
	 * @param clazz
	 * @param requestCode
	 */
	protected void openActivity(Class<?> clazz, int requestCode) {
		try {
			Intent intent = new Intent(getActivity(), clazz);
			startActivityForResult(intent, requestCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 显示 Toast
	 * @param resid
	 */
	protected void showToast(int resid) {
		ToastUtil.showShort(getActivity(), resid, getToastStyle(), R.id.ll_toast);
	}
	
	/**
	 * 显示 Toast
	 * @param msg
	 */
	protected void showToast(String msg) {
		ToastUtil.showShort(getActivity(), msg, getToastStyle(), R.id.ll_toast);
	}
	
	/**
	 * 获取 Toast 样式
	 * @return
	 */
	protected Style getToastStyle() {
		switch (ThemeUtil.getCurTheme()) {
		case ThemeUtil.THEME_BULE:
			return Style.BLUE;
		case ThemeUtil.THEME_NIGHT:
			return Style.NIGHT;
		default:
			return Style.BLUE;
		}
	}
	
	/**
	 * 
	 * @param url
	 * @param l
	 */
	protected void get(String url, RequestListener l) {
		get(url, null, l);
	}
	
	/**
	 * 
	 * @param url
	 * @param params
	 * @param l
	 */
	protected void get(String url, RequestParams params, RequestListener l) {
		RequestManager.get(url, this, params, l);
	}
	
	/**
	 * 
	 * @param url
	 * @param l
	 */
	protected void post(String url, RequestListener l) {
		post(url, null, l);
	}
	
	/**
	 * 
	 * @param url
	 * @param params
	 * @param l
	 */
	protected void post(String url, RequestParams params, RequestListener l) {
		RequestManager.post(url, this, params, l);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
	}
	
	@Override
	protected void onDestroy() {
		ActivityUtil.remove(this);
		RequestManager.cancelAll(this);
		super.onDestroy();
	}
}
